package com.android.delivery.receiver;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.android.delivery.R;

/**
 * Desc:
 * <p>
 * Author: jiangwenxin
 * Date: 2019/12/5
 * Copyright: Copyright (c) 2010-2018
 * Company: @咪咕动漫
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class PushNotificationUtil {
    private static PushNotificationUtil mPushNotificationUtil = null;

    private void PushNotificationUtil() {

    }

    public static PushNotificationUtil getInstance() {
        if (mPushNotificationUtil == null) {
            mPushNotificationUtil = new PushNotificationUtil();
        }
        return mPushNotificationUtil;
    }

    public static final String PUSH_CHANNEL_ID = "cn.delivery.push.receiver";
    public static final String PUSH_CHANNEL_NAME = "山村推送";

    public Notification createPushNotification(Context context, NotificationManager notificationManager,
                                               String channelId, String channelName, String title, String msg, Integer badgeCount) {
        try {
            Notification notification;
            Notification.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
                mChannel.enableVibration(true);
                mChannel.setShowBadge(true);
                notificationManager.createNotificationChannel(mChannel);
                builder = new Notification.Builder(context, channelId);
                if (badgeCount != null && AppBadgeUtil.isSupportRealBadgeDevice()) {
                    builder.setNumber(badgeCount);
                }
                builder.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
            } else {
                builder = new Notification.Builder(context);
            }
            builder.setContentTitle(title);
            builder.setContentText(msg);
            builder.setSmallIcon(R.mipmap.ic_logo);

            builder.setAutoCancel(true);
            /**
             * app图标若有替换，需要同步替换
             * 1.tsg服务端设置的华为推送app图标
             * （目前配置链接http://images.migudm.cn/group1/M00/40/6A/CjB8FF7XBSGAcWhqAAA4FGmHBAs663.png）
             * 2.魅族推送后台，上传的app图标
             */
            builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_logo));
            builder.setShowWhen(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                notification = builder.build();
            } else {
                notification = builder.getNotification();
            }
            //根据厂商自定义角标数量
            if (badgeCount != null) {
                AppBadgeUtil.setNotificationBadgeCount(context, notification, badgeCount);
            }
            return notification;
        } catch (Exception e) {
            return null;
        }
    }


}
